package com.hanbang.chu_xing_gao_zhao.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.chu_xing_gao_zhao.application.ActivityManager;
import com.hanbang.chu_xing_gao_zhao.application.MyApplication;
import com.hanbang.chu_xing_gao_zhao.entity.UserInforData;
import com.hanbang.chu_xing_gao_zhao.module.LogInActivity;
import com.hanbang.chu_xing_gao_zhao.utils.BitmapUtil;
import com.hanbang.chu_xing_gao_zhao.utils.DbUtil;
import com.hanbang.chu_xing_gao_zhao.utils.MyLog;
import com.hanbang.chu_xing_gao_zhao.utils.MySharedPreferences;
import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.utils.UiUtils;
import com.hanbang.cxgz_2.R;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.DbException;

import java.util.ArrayList;

public class BaseFragmentActivity extends FragmentActivity {
	private String TAG = BaseActivity.class.getSimpleName();
	public BitmapUtils bitmapUtils = null;
	public HttpUtils httpUtils = null;
	public DbUtils dbUtils = null;
	public UserInforData userData = null;
	private boolean isLoadFont = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		bitmapUtils = BitmapUtil.getBitmapUtils(BaseFragmentActivity.this,
				MyApplication.bitmapCachePath);
		httpUtils = ((MyApplication) getApplicationContext()).httpUtils;
		dbUtils = DbUtil.getDbUtils(this);
		getDbUserData();
		ActivityManager.getInstance().pushActivity(this);
	}

	/*
	 * 设置回退键
	 */
	public void setBackArrows(View back) {
		back.setVisibility(View.VISIBLE);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	public void getDbUserData() {
		int id = MySharedPreferences.getSharedPreferencesKeyAndValue(this,
				MySharedPreferences.USER_DATA, "user_id", -1);
		if (id != -1) {
			try {
				userData = DbUtil.getDbUtils(this).findById(
						UserInforData.class, id);
			} catch (DbException e) {
				e.printStackTrace();
				userData = null;
			}
		} else {
			userData = null;
		}
	}

	public void setDbUserData() {
		if (userData != null) {
			try {
				DbUtil.getDbUtils(this).saveOrUpdate(userData);
			} catch (DbException e) {
				e.printStackTrace();
			}
		}
	}

	public boolean isLogin(boolean isToLogin) {
		if (userData == null) {
			if (isToLogin) {
				Intent intent = new Intent(this, LogInActivity.class);
				startActivity(intent);
			}
			MyLog.e(TAG, "未登录");
			return false;
		} else {
			MyLog.e(TAG, "已登录");
			return true;
		}
	}

	public void passwordIsError(boolean isToast) {
		MySharedPreferences.setSharedPreferencesKeyAndValue(
				BaseFragmentActivity.this, MySharedPreferences.USER_DATA,
				"user_id", -1);
		userData = null;
		if (isToast) {
			MyToast.show(this, "用户名或密码错误,请重新登录!", Toast.LENGTH_SHORT);
		}
	}

	/*
	 * 设置回退键
	 */
	public void setTitle(View title, String t) {

		title.setVisibility(View.VISIBLE);
		((TextView) title).setText(t);
	}



	/**
	 * 设置标题与返回搜索按钮的显示,名字最多传入4个
	 *
	 * @param back
	 *            设置返回按钮是否显示
	 * @param title
	 *            设置标题
	 * @param search
	 *            设置探索按钮是否显示
	 */
	public void setTitleMore(int back, int search, String title1, String title2, String title3, String title4) {
		ArrayList<TextView> textViewts = new ArrayList<TextView>();
		ArrayList<String> title = new ArrayList<String>();

		findViewById(R.id.bar_top_conver_classify_back).setVisibility(back);
		findViewById(R.id.im_bar_top_conver_classify_search).setVisibility(search);

		title.add(title1);
		title.add(title2);
		title.add(title3);
		title.add(title4);

		textViewts.add((TextView) findViewById(R.id.tv_bar_top_conver_classify_title1));
		textViewts.add((TextView) findViewById(R.id.tv_bar_top_conver_classify_title2));
		textViewts.add((TextView) findViewById(R.id.tv_bar_top_conver_classify_title3));
		textViewts.add((TextView) findViewById(R.id.tv_bar_top_conver_classify_title4));

		for (int i = 0; i < textViewts.size(); i++) {
			if (title.get(i) != null) {

				textViewts.get(i).setText(title.get(i));
			} else {
				textViewts.get(i).setVisibility(View.GONE);
			}
		}

	}




	@Override
	protected void onResume() {
		super.onResume();
		getDbUserData();
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (isLoadFont) {
			UiUtils.applyFont(UiUtils.getRootView(this));
			isLoadFont = false;
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ActivityManager.getInstance().exitActivity(this);
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		ActivityManager.getInstance().exitActivity(this);
	}

}
